package model 
{
	import controller.Controller;
	import events.ViewEvent;
	import flash.display.Sprite;
	import org.osmf.events.DynamicStreamEvent;
	import org.osmf.events.MediaPlayerCapabilityChangeEvent;
	import org.osmf.events.TimeEvent;
	import org.osmf.layout.LayoutMetadata;
	import org.osmf.media.MediaPlayer;
	import org.osmf.net.NetLoader;
	import events.ControllerEvent;
	import utils.Strings;
	
	import org.osmf.containers.MediaContainer;
	import org.osmf.events.MediaPlayerStateChangeEvent;
	import org.osmf.layout.HorizontalAlign;
	import org.osmf.layout.ScaleMode;
	import org.osmf.layout.VerticalAlign;
	import org.osmf.media.MediaElement;
	import org.osmf.media.MediaPlayerState;
	
	import org.osmf.net.DynamicStreamingItem;
	import org.osmf.net.DynamicStreamingResource;
	
	import org.osmf.media.MediaPlayerSprite;
	import org.osmf.elements.VideoElement;
	import org.osmf.media.URLResource;
	import org.osmf.utils.URL;
	
	/**
	 * ...
	 * @author Henrik Riise Hansen
	 */
	
	//http://flexibleair.wordpress.com/2010/03/28/introduction_to_osmf_en/
	//http://admin.ovp.qbrick.com/Login.aspx?ReturnUrl=%2fcustomers.aspx
	
	public class Player extends Sprite 
	{
		private var control:Controller;
		private var shell:Shell;
		
		private var player:MediaPlayer;
		private var container:MediaContainer;
		private var videoElement : VideoElement ;
		
		
		public function Player(_shell:Shell,_controller:Controller)
		{
			shell = _shell;
			
			control = _controller;
			control.addEventListener(ViewEvent.PLAY, onPlay);
			control.addEventListener(ViewEvent.PAUSE, onPause);
			control.addEventListener(ViewEvent.SEEK, onSeek);
			
			init();
		}
		
		private function init():void
		{
			player = new MediaPlayer ( ) ;
			
			player.autoRewind = true;
			player.autoPlay = true;
			
			player.addEventListener(MediaPlayerStateChangeEvent.MEDIA_PLAYER_STATE_CHANGE, onPlayerState);
			player.addEventListener(TimeEvent.CURRENT_TIME_CHANGE, onPlayerCurrentTimeChange);
			
			container = new MediaContainer ( ) ;
			container.layoutMetadata.scaleMode = ScaleMode.LETTERBOX;
			container.layoutMetadata.width = 674;
			container.layoutMetadata.height = 409;
			container.layoutMetadata.percentHeight = 100;
			container.layoutMetadata.percentWidth = 100;
			container.layoutMetadata.horizontalAlign = HorizontalAlign.CENTER;
			container.layoutMetadata.verticalAlign = VerticalAlign.MIDDLE;
			container.backgroundColor = 0x000000;
			container.backgroundAlpha = 1;
			container.clipChildren = true;
			
			addChild(container);
			
			var resource : DynamicStreamingResource = new DynamicStreamingResource ( "rtmp://cp67126.edgefcs.net/ondemand" ) ;
			var vector : Vector.<DynamicStreamingItem> = new Vector.<DynamicStreamingItem> ( 3 ) ;
			vector [ 0 ] = new DynamicStreamingItem ( "mp4:mediapm/ovp/content/demo/video/elephants_dream/elephants_dream_1280x720_24.0fps_1708kbps" , 1708 ) ; 
			vector [ 1 ] = new DynamicStreamingItem ( "mp4:mediapm/ovp/content/demo/video/elephants_dream/elephants_dream_1024x522_24.0fps_908kbps" , 908 ) ; 
			vector [ 2 ] = new DynamicStreamingItem ( "mp4:mediapm/ovp/content/demo/video/elephants_dream/elephants_dream_768x428_24.0fps_408kbps" , 408 ) ; 
			resource.streamItems = vector ;
			videoElement = new VideoElement( resource ) ;
			
			var layoutMetadata:LayoutMetadata = new LayoutMetadata();
			layoutMetadata.scaleMode = ScaleMode.STRETCH;
			layoutMetadata.width = 674;
			layoutMetadata.height = 409;
			layoutMetadata.horizontalAlign = HorizontalAlign.CENTER;
			layoutMetadata.verticalAlign = VerticalAlign.MIDDLE;
			
			videoElement.addMetadata(LayoutMetadata.LAYOUT_NAMESPACE, layoutMetadata);
			player.media = videoElement ;
			container.addMediaElement ( videoElement ) ;	
		}
		
		private function onPlayerCurrentTimeChange(e:TimeEvent):void 
		{
			var downloadProcent:Number = player.bytesLoaded / player.bytesTotal;
			
			trace(player.bytesLoaded);
			
			dispatchEvent(new ControllerEvent(ControllerEvent.PROGRESS, { time:e.time, duration:player.duration } ));	
		}
		
		private function onPlayerState(e:MediaPlayerStateChangeEvent):void
		{
			dispatchEvent(new ControllerEvent(ControllerEvent.STATE, { state:e.state } ));
		}
		
		private function onPlay(e:ViewEvent):void
		{
			if (player.canPlay)
			{
				player.play();
			}
		}
		
		private function onPause(e:ViewEvent):void
		{
			if (player.canPause)
			{
				player.pause();
			}
		}
		
		private function onSeek(e:ViewEvent):void
		{
			var seekTime:Number = player.duration * e.data.seek/409;
			
			if (player.canSeek && player.canSeekTo(seekTime))
			{
				player.seek(seekTime);
			}
		}
	}
}